﻿namespace MicroCloud.AspNetCore
{
    /// <summary>
    /// 基于当前HttpContext的<see cref="IServiceScope"/>的异步任务取消标识
    /// </summary>
    public class HttpContextCancellationTokenProvider : ICancellationTokenProvider
    {
        //字段
        private readonly IHttpContextAccessor _httpContextAccessor;

        #region "属性"
        #region "获取 异步任务取消标识"
        /// <summary>
        /// 获取 异步任务取消标识
        /// </summary>
        public CancellationToken Token => _httpContextAccessor.HttpContext?.RequestAborted ?? CancellationToken.None;
        #endregion

        #endregion

        #region "构造函数"
        #region "初始化一个<see cref="HttpContextCancellationTokenProvider"/>类型的新实例"
        /// <summary>
        /// 初始化一个<see cref="HttpContextCancellationTokenProvider"/>类型的新实例
        /// </summary>
        /// <param name="httpContextAccessor"></param>
        public HttpContextCancellationTokenProvider(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        #endregion

        #endregion

    }

}
